<?php
/*
 * @Description: file content
 * @FilePath: \auxface_certificate\app\certificate\controller\admin\v1\machineRisk\Type.php
 */

declare (strict_types = 1);

namespace app\certificate\controller\admin\v1\machineRisk;

use think\facade\Db;
use think\Request;

class Type
{
    /**
     * @Description:
     * @param {Request} $request
     */
    public function getList(Request $request)
    {
        $data  = $request->data;
        $limit = $data['limit'];
        $page  = $data['page'];

        $where = [
            ['projectCode', '=', $request->prjCode],
            ['isDelete', '=', 0],
        ];

        if (!empty($data['name'])) {
            array_push($where, ['name', 'like', $data['name'] . "%"]);
        }

        $list = DB::table('certificate_machineRiskType')
            ->where($where)
            ->field(['name', 'remark', 'inDate', 'uuid', 'flowUuid', 'subFormUuids'])
            ->order('id desc')
            ->page((int) $page, (int) $limit)
            ->select()->toArray();

        foreach ($list as &$item) {
            // $item['fileTypeUuids'] = json_decode($item['fileTypeUuids'], true) ?: [];
            // $item['fileTypeUuidsNums'] = count($item['fileTypeUuids']);
            $item['subFormUuids'] = json_decode($item['subFormUuids'], true) ?: [];
            $item['subFormUuids'] = DB::table('certificate_subform')->where([
                ['uuid', 'in', $item['subFormUuids']],
                ['isDelete', '=', 0],
            ])->column('uuid');
            $item['subFormUuidsNums'] = count($item['subFormUuids']);
        }

        $count = DB::table('certificate_machineRiskType')
            ->where($where)
            ->count();

        $data = compact('list', 'count');

        return result(SUCCESS, $data);
    }
    /**
     * @Description:
     * @param {Request} $request
     */
    public function create(Request $request)
    {
        $data = $request->data;

        $insert = [
            'uuid'          => createUuid(),
            'projectCode'   => $request->prjCode,
            'corpCode'      => $request->corpCode,
            'name'          => $data['name'],
            'remark'        => $data['remark'] ?? '',
            'flowUuid'      => $data['flowUuid'] ?? '',
            'subFormUuids'  => json_encode($data['subFormUuids'] ?? [], JSON_UNESCAPED_UNICODE),
            'subtitle'      => $data['subtitle'] ?? '',
            'type'          => $data['type'] ?? '',
            'closeFlowUuid' => $data['closeFlowUuid'] ?? '',
        ];
        DB::table('certificate_machineRiskType')->insert($insert);

        return result(SUCCESS, ['uuid' => $insert['uuid'], 'inDate' => date('Y-m-d H:i:s')], '添加成功!');
    }

    /**
     * @Description: 修改
     */
    public function update(Request $request)
    {
        $data = $request->data;
        $uuid = $data['uuid'];

        $cehck = DB::table('certificate_machineRiskType')->where([
            ['uuid', '<>', $uuid],
            ['name', '=', $data['name']],
            ['isDelete', '=', 0],
        ])->value('id');
        if ($cehck) {
            return result(FAIL, '', '名称已存在,无法修改!');
        }

        $updateData = [
            'name'          => $data['name'],
            'remark'        => $data['remark'] ?? '',
            'flowUuid'      => $data['flowUuid'] ?? '',
            'subFormUuids'  => json_encode($data['subFormUuids'] ?? [], JSON_UNESCAPED_UNICODE),
            'subtitle'      => $data['subtitle'] ?? '',
            'type'          => $data['type'] ?? '',
            'closeFlowUuid' => $data['closeFlowUuid'] ?? '',
        ];
        DB::table('certificate_machineRiskType')->where('uuid', $uuid)->update($updateData);
        return result(SUCCESS, '', '更改成功');
    }

    public function destroy(Request $request)
    {
        $data = $request->data;
        DB::table('certificate_machineRiskType')->where('uuid', $data['uuid'])->update(['isDelete' => 1]);
        return result(SUCCESS, '', '删除成功');
    }

    /**
     * @Description: 获取数据
     * @param {Request} $request
     */
    public function getDetail(Request $request)
    {
        $data = $request->data;
        $uuid = $data['uuid'];

        $data                 = DB::table('certificate_machineRiskType')->where('uuid', $uuid)->field(['inDate', 'name', 'remark', 'uuid', 'flowUuid', 'subFormUuids', 'subtitle'])->find();
        $data['subFormUuids'] = json_decode($data['subFormUuids'], true);
        return result(SUCCESS, $data, '');
    }

    /**
     * @Description: 修改附件类型
     */
    // public function changeFileType(Request $request)
    // {
    //     $data = $request->data;
    //     $uuid = $data['uuid'];

    //     $info = DB::table('certificate_machineRiskType')->where([
    //         ['uuid', '=', $uuid],
    //         ['isDelete', '=', 0],
    //     ])->find();
    //     if (!$info) return result(FAIL, '', '流程表单不存在!');
    //     $updateData = [
    //         'fileTypeUuids' => json_encode($data['fileTypeUuids'], JSON_UNESCAPED_UNICODE),
    //     ];
    //     DB::table('certificate_machineRiskType')->where('uuid', $uuid)->update($updateData);
    //     return result(SUCCESS, '', '更改成功');
    // }

    /**
     * @Description: 修改附表
     */
    public function changeSubForm(Request $request)
    {
        $data = $request->data;
        $uuid = $data['uuid'];

        $info = DB::table('certificate_machineRiskType')->where([
            ['uuid', '=', $uuid],
            ['isDelete', '=', 0],
        ])->find();
        if (!$info) {
            return result(FAIL, '', '流程表单不存在!');
        }

        $updateData = [
            'subFormUuids' => json_encode($data['subFormUuids'], JSON_UNESCAPED_UNICODE),
        ];
        DB::table('certificate_machineRiskType')->where('uuid', $uuid)->update($updateData);
        return result(SUCCESS, '', '更改成功');
    }

    /**
     * @Description: 获取字典
     * @param {Request} $request
     */
    public function getOptions(Request $request)
    {
        $where = [
            ['projectCode', '=', $request->prjCode],
            ['isDelete', '=', 0],
        ];

        $data['flowList'] = DB::table('certificate_flow')
            ->where($where)
            ->field(['name', 'remark', 'uuid'])
            ->order('id desc')
            ->select();

        $data['fileTypeList'] = DB::table('certificate_fileType')
            ->where($where)
            ->field(['name', 'remark', 'uuid'])
            ->order('id desc')
            ->select();

        $data['subformList'] = DB::table('certificate_subform')
            ->where($where)
            ->field(['name', 'remark', 'uuid'])
            ->order('id desc')
            ->select();

        $data['machineRiskTypeList'] = DB::table('certificate_machineRiskType')
            ->where($where)
            ->field(['name', 'remark', 'uuid'])
            ->order('id desc')
            ->select();

        return result(SUCCESS, $data, '');
    }
}
